package br.ufrn.sipac.patrimonio.coletor.helper;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import br.ufrn.sipac.patrimonio.coletor.dominio.Bem;
import br.ufrn.sipac.patrimonio.coletor.dominio.FinalidadeLevantamento;
import br.ufrn.sipac.patrimonio.coletor.dominio.ItemLevantamento;
import br.ufrn.sipac.patrimonio.coletor.dominio.Levantamento;
import br.ufrn.sipac.patrimonio.coletor.dominio.Localidade;
import br.ufrn.sipac.patrimonio.coletor.dominio.Persistencia;
import br.ufrn.sipac.patrimonio.coletor.dominio.Unidade;
import br.ufrn.sipac.patrimonio.coletor.dominio.UsuarioColetor;
import br.ufrn.sipac.patrimonio.coletor.exception.ColetorRuntimeException;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * 
 * @author Giuliana Silva Bezerra
 * @author Alisson Nascimento
 * @author Alessandro Marinho
 * 
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	// name of the database file for your application -- change to something
	// appropriate for your app
	public static final String DATABASE_NAME = "coletor_patrimonio_16.db";
	// Any time you make changes to your database objects, you may have to
	// increase the database version
	public static final int DATABASE_VERSION = 1;

	// the DAO object we use to access the SimpleData table
	private Dao<Persistencia, Integer> unidadeDAO = null;
	private Dao<Persistencia, Integer> finalidadeDAO = null;
	private Dao<Persistencia, Integer> localidadeDAO = null;
	private Dao<Persistencia, Integer> bemDAO = null;
	private Dao<Persistencia, Integer> itemDAO = null;
	private Dao<Persistencia, Integer> usuarioDAO = null;
	private Dao<Persistencia, Integer> levantamentoDAO = null;

	// we do this so there is only one helper
	private static DatabaseHelper helper = null;

	private DatabaseHelper(Context context) {

		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * Get the helper, possibly constructing it if necessary. For each call to
	 * this method, there should be 1 and only 1 call to {@link #close()}.
	 */
	public static synchronized DatabaseHelper getHelper(Context context) {
		if (helper == null) {
			helper = new DatabaseHelper(context);
		}
		return helper;
	}

	/**
	 * This is called when the database is first created. Usually you should
	 * call createTable statements here to create the tables that will store
	 * your data.
	 */
	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onCreate");
			TableUtils.createTable(connectionSource, Unidade.class);
			TableUtils.createTable(connectionSource, Localidade.class);
			TableUtils.createTable(connectionSource, UsuarioColetor.class);
			TableUtils.createTable(connectionSource,
					FinalidadeLevantamento.class);
			TableUtils.createTable(connectionSource, Levantamento.class);
			TableUtils.createTable(connectionSource, Bem.class);
			TableUtils.createTable(connectionSource, ItemLevantamento.class);
		} catch (SQLException e) {
			throw new ColetorRuntimeException(e.getMessage());
		}
	}

	/**
	 * This is called when your application is upgraded and it has a higher
	 * version number. This allows you to adjust the various data to match the
	 * new version number.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int oldVersion, int newVersion) {
		// try {
		// Log.i(DatabaseHelper.class.getName(), "onUpgrade");
		// TableUtils.dropTable(connectionSource, Unidade.class, true);
		// // after we drop the old databases, we create the new ones
		// onCreate(db, connectionSource);
		// } catch (java.sql.SQLException e) {
		// e.printStackTrace();
		// }
	}

	/**
	 * Returns the Database Access Object (DAO) for our SimpleData class. It
	 * will create it or just give the cached value.
	 * 
	 * @throws java.sql.SQLException
	 */
	public Dao<Persistencia, Integer> getUnidadeDAO() throws SQLException {
		if (unidadeDAO == null) {
			unidadeDAO = getDao(Unidade.class);
		}
		return unidadeDAO;
	}

	public Dao<Persistencia, Integer> getFinalidadeDAO() throws SQLException {
		if (finalidadeDAO == null) {
			finalidadeDAO = getDao(FinalidadeLevantamento.class);
		}
		return finalidadeDAO;
	}

	public Dao<Persistencia, Integer> getLocalidadeDAO() throws SQLException {
		if (localidadeDAO == null) {
			localidadeDAO = getDao(Localidade.class);
		}
		return localidadeDAO;
	}

	public Dao<Persistencia, Integer> getBemDAO() throws SQLException {
		if (bemDAO == null) {
			bemDAO = getDao(Bem.class);
		}
		return bemDAO;
	}

	public Dao<Persistencia, Integer> getItemDAO() throws SQLException {
		if (itemDAO == null) {
			itemDAO = getDao(ItemLevantamento.class);
		}
		return itemDAO;
	}

	public Dao<Persistencia, Integer> getUsuarioDAO() throws SQLException {
		if (usuarioDAO == null)
			usuarioDAO = getDao(UsuarioColetor.class);
		return usuarioDAO;
	}

	public Dao<Persistencia, Integer> getLevantamentoDAO() throws SQLException {
		if (levantamentoDAO == null)
			levantamentoDAO = getDao(Levantamento.class);
		return levantamentoDAO;
	}

	/**
	 * Close the database connections and clear any cached DAOs. For each call
	 * to {@link #getHelper(Context)}, there should be 1 and only 1 call to this
	 * method. If there were 3 calls to {@link #getHelper(Context)} then on the
	 * 3rd call to this method, the helper and the underlying database
	 * connections will be closed.
	 */
	@Override
	public void close() {
		super.close();
		finalidadeDAO = null;
		helper = null;
	}

}
